#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    leds = new fsmpLeds();
    leds->off(leds->LED1);
    leds->off(leds->LED2);
    leds->off(leds->LED3);
    for(int i = 0;i<3;i++)
        flag3[i] = false;
    tem = new fsmpTempHum(this);
    mqtt_cli = new QMqttClient(this);
    tcp_server = new QTcpServer(this);
    tcp_client_socket = new QTcpSocket(this);
    tcp_client_socket = nullptr;
    tcp_server->listen(QHostAddress::AnyIPv4,8888);  //开始监听

    connect(tcp_server, &QTcpServer::newConnection, this, [=]() {
        // 当单片机连接时，获取它的套接字
        tcp_client_socket = tcp_server->nextPendingConnection();
        qDebug() << "Client connected from:" << tcp_client_socket->peerAddress().toString();
    });

    temp_show();  //只在初始化时测试一次温度，还没有实时测温
    init_commodity_img();
    init_mqtt_cli();

    connect(mqtt_cli, &QMqttClient::connected, this, [=](){
         qDebug() << "Connect to MQTT server success!";
    });

    mqtt_cli->connectToHost();

}

Widget::~Widget()
{
    delete ui;
}
void Widget::init_mqtt_cli(){
    mqtt_cli->setHostname("bemfa.com");
    mqtt_cli->setPort(9501);
    mqtt_cli->setClientId("2f2f323f727b4d50aac21dbbc3c9aba1");
}
void Widget::init_commodity_img(){

    ui->img1->setScaledContents(true);
    ui->img1->setPixmap(QPixmap(":/img_resources/cola.jpg"));

    ui->img2->setScaledContents(true);
    ui->img2->setPixmap(QPixmap(":/img_resources/water.png"));

    ui->img3->setScaledContents(true);
    ui->img3->setPixmap(QPixmap(":/img_resources/xuebi.jpg"));


}
void Widget::temp_show(){

    double temp = tem->temperature();
    qDebug()<<"temp:"<<temp;
    QString temp_1 = QString::number(temp,'f',2);
    ui->temp_show->display(temp_1);

}
void Widget::on_led1_btn_clicked()
{
    if(!flag3[0])
    {
        leds->on(leds->LED1);
        flag3[0] = !flag3[0];
    }
    else
    {
        leds->off(leds->LED1);
        flag3[0] = !flag3[0];
    }
}

void Widget::on_led2_btn_clicked()
{
    if(!flag3[1])
    {
        leds->on(leds->LED2);
        flag3[1] = !flag3[1];
    }
    else
    {
        leds->off(leds->LED2);
        flag3[1] = !flag3[1];
    }

}


void Widget::on_led3_btn_clicked()
{
    if(!flag3[2])
    {
        leds->on(leds->LED3);
        flag3[2] = !flag3[2];
    }
    else
    {
        leds->off(leds->LED3);
        flag3[2] = !flag3[2];
    }

}

void Widget::on_led4_btn_clicked(){}


void Widget::on_select1_clicked()
{
    if (mqtt_cli->state() == QMqttClient::Connected) {
           mqtt_cli->publish(QMqttTopicName("VhPfd18fB006"),"channel_1");
           qDebug()<<"select1";
       } else {
           qDebug() << "MQTT not connect";
       }

    if (tcp_client_socket && (tcp_client_socket->state() == QTcpSocket::ConnectedState)) {
            qint64 bytesWritten = tcp_client_socket->write("channel_1_choose");
            tcp_client_socket->flush(); // 立即发送
            if (bytesWritten == -1) {
                qDebug() << "Failed to send to client:" << tcp_client_socket->errorString();
            } else {
                qDebug() << "Sent" << bytesWritten << "bytes to client";
            }
     }
    else{
            qDebug() << "No client connected or socket not ready";
     }

}

void Widget::on_select2_clicked()
{
    if (mqtt_cli->state() == QMqttClient::Connected) {
           mqtt_cli->publish(QMqttTopicName("xRd6ztoK6006"),"channel_2");
           qDebug()<<"select1";
       } else {
           qDebug() << "MQTT not connect";
       }
}


void Widget::on_select3_clicked()
{
    if (mqtt_cli->state() == QMqttClient::Connected) {
           mqtt_cli->publish(QMqttTopicName("BiNTKMmmA006"),"channel_3");
           qDebug()<<"select1";
       } else {
           qDebug() << "MQTT not connect";
       }
}

